<template>
  <div>
    <div v-if="orders != null">
      <div v-if="orders.orderStatus == 2">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>未完成订单</span>
          </div>
          <div class="text item">
            {{ "订单编号:" + orders.orderNumber }} <br />
            {{ "订单时间:" + orders.ordderTime }} <br />
            {{ "订单状态:" + "进行中..." }}
          </div>
        </el-card>
      </div>
    </div>

    <el-button
      type="primary"
      round
      style="margin: 200px"
      :disabled="pvalue"
      @click="inOrder"
      >点击下单</el-button
    >
    <el-dialog title="选择租用床位" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="医院名称"
          :label-width="formLabelWidth"
          prop="hosid"
        >
          <el-select
            v-model="ruleForm.hosid"
            @change="getBookPageList"
            placeholder="请选择所在医院"
          >
            <el-option
              v-for="item in hospitals"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="科室名称"
          :label-width="formLabelWidth"
          prop="deptid"
        >
          <el-select
            v-model="ruleForm.deptid"
            @change="getBookPageList"
            placeholder="请选择所在科室"
          >
            <el-option
              v-for="item in depts"
              :key="item.id"
              :label="item.deptname"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="床位编号" :label-width="formLabelWidth" prop="id">
          <el-select v-model="ruleForm.id" placeholder="请选择床位编号">
            <el-option
              v-for="item in beds"
              :key="item.id"
              :label="item.id"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item
          label="应付租金"
          :label-width="formLabelWidth"
         
        >
          <el-input
            v-model="orderspo.rent"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
   -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-dialog>
    <el-button
      type="success"
      round
      style="margin-top: 200px; margin-left: 0px"
      :disabled="!pvalue"
      @click="outOrder"
      >点击归还</el-button
    >
    <el-dialog title="订单结算" :visible.sync="dialogFormVisibleone">
      <el-form :model="orderspo">
        <el-form-item label="订单编号" :label-width="formLabelWidth">
          <el-input
            v-model="orderspo.orderNumber"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="租用时常" :label-width="formLabelWidth">
          <el-input
            v-model="orderspo.leaseTime"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="支付方式" :label-width="formLabelWidth">
          <el-radio v-model="radio" value="1" label="1">支付宝</el-radio>
          <el-radio v-model="radio" value="2" label="2">微信</el-radio>
        </el-form-item>
        <el-form-item label="应付金额" :label-width="formLabelWidth">
          <el-input
            v-model="orderspo.payMoney"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleone = false" icon="el-icon-back"
          >取 消</el-button
        >
        <el-button type="primary" icon="el-icon-circle-check" @click="subint"
          >支 付</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      // name: "",
      url: "http://localhost:8080/jiapei/orders/displayall",
      url1: "http://localhost:8080/jiapei/orders/saveorders",
      url2: "http://localhost:8080/jiapei/orders/look",
      url3: "http://localhost:8080/jiapei/orders/return",
      hospitals: [],
      bed: [],
      depts: [],
      orders: {},
      lastorders:{},
      hosname: "",
      deptname: "",
      id: "",
      deptid: "",
      hosid: "",
      orderspo: "",
      radio: "1",
      leaseTime: "",
      payMoney: "",
      dialogFormVisible: false,
      dialogFormVisibleone: false,
      rent: "",
      pvalue: false,
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      ruleForm: {
        name: "",
        region: "",
        hosname: "",
        id: "",
        deptname: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
        rent: "",
      },
      rules: {
        hosid: [
          { required: true, message: "请选择活动区域", trigger: "change" },
        ],
        id: [{ required: true, message: "请选择活动区域", trigger: "change" }],
        deptid: [
          { required: true, message: "请选择活动区域", trigger: "change" },
        ],
        rent: [
          { required: true, message: "请选择活动区域", trigger: "change" },
        ],
      },
      formLabelWidth: "120px",
    };
  },
  name: "Dashboard",
  computed: {
    ...mapGetters(["name"]),
  },
  mounted() {
    this.getBookPageList();
  },
  methods: {
    subint() {
      this.pvalue = false;
      this.dialogFormVisibleone = false;
      // console.log(this.orderspo.payMoney);
      console.log(this.orderspo.leaseTime);
      this.$axios({
        method: "GET",
        url: this.url3,
        params: {
          id: this.orders.id,
          radio: this.radio,
          leaseTime: this.orderspo.leaseTime,
          payMoney: this.orderspo.payMoney,
        },
      }).then((res) => {
        this.lastorders = res.data.data.lastorder
        if (res.data.code == 200) {
          this.$message("结算成功");
          this.getBookPageList();
        }
        
        this.$confirm("是否进行反馈?", "提示", {
          confirmButtonText: "反馈",
          cancelButtonText: "订单结束",
          type: "warning",
        })
          .then(() => {
            this.$router.push({
              path: "/orders/addfeedback",
              query: {
                id: this.lastorders.id,
                mobile:this.lastorders.mobile,
                cusId:this.lastorders.cusId,
              },
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "订单已结束",
            });
          });
      });
    },

    inOrder() {
      this.dialogFormVisible = true;
    },
    outOrder() {
      this.dialogFormVisibleone = true;
      this.$axios({
        method: "GET",
        url: this.url2,
        params: {
          id: this.orders.id,
        },
      }).then((res) => {
        this.orderspo = res.data.data.orderspo;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        this.pvalue = true;
        if (valid) {
          // alert("submit!");
          this.dialogFormVisible = false;
          this.$axios({
            method: "GET",
            url: this.url1,
            params: {
              id: this.ruleForm.id,
              hosid: this.ruleForm.hosid,
              deptid: this.ruleForm.deptid,
              name: this.name,
            },
          }).then((res) => {
            this.status = res.data.data.orders;
            if (res.data.code == 200) {
              this.$message("下单成功");
              this.getBookPageList();
            }
            if (res.data.code == 201) {
              this.$message("下单失败，请完成未完成的订单");
              this.getBookPageList();
            }
            //this.code = res.data.data.code;
            this.$refs[formName].resetFields();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    getBookPageList() {
      this.$axios({
        method: "GET",
        url: this.url,
        params: {
          // id: this.id,
          // name: this.name,
          // deptname: this.deptname,
          deptid: this.ruleForm.deptid,
          hosid: this.ruleForm.hosid,
          name: this.name,
        },
      }).then((res) => {
        this.hospitals = res.data.data.hospitals;
        console.log(res.data.data.hospitals);
        this.depts = res.data.data.depts;
        this.beds = res.data.data.beds;
        this.orders = res.data.data.serviceOne;
        console.log(res.data.data.serviceOne);
        if (this.orders != null) {
          if (this.orders.orderStatus == 2) {
            this.pvalue = true;
          }
        }
      });
    },
  },
};
</script>

<style>
.text {
  font-size: 16px;
}

.item {
  margin-bottom: 5px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
  color: #000;
}

.box-card {
  width: 1080th;
}
</style>